define(['app'], function(app) {
    'use strict';
    /*权限管理*/
    app.controller('Right', function($scope, dataFactoryCRM, $q) {
        $scope.rightlist;
        $scope.rightcfgs;
        $scope.add_data = { right_name: '', code: '', info: '', rights: {} };
        $scope.edit_data = { right_id: 0, right_name: '', code: '', info: '', rights: {} };
        $scope.rightspage = { pageAll: 1, pageNow: 1, pageSize: $scope.psize, k: 3335 };


        $scope.getDataList = function() {
            $scope.busy = dataFactoryCRM.getDataByAPI('right/index?psize=' + $scope.rightspage.pageSize + '&p=' + $scope.rightspage.pageNow)
                .then(
                    function(d) {
                        $scope.rightlist = d.data;
                    });
        }

        $scope.initParam = function() {
            $scope.busy = dataFactoryCRM.getDataByAPI('right/getconfig').then(
                function(d) {
                    $scope.rightcfgs = d.data;
                });
        }

        $scope.addchk = function(key, v) {
            var vitem = {};
            vitem.code = v.code;
            vitem.model = v.model;
            vitem.action = v.action;

            if ($scope.add_data.rights[vitem.code])
                delete $scope.add_data.rights[vitem.code];
            else
                $scope.add_data.rights[vitem.code] = vitem;
        }

        $scope.editchk = function(key, v) {
            var vitem = {};
            vitem.code = v.code;
            vitem.model = v.model;
            vitem.action = v.action;
            if ($scope.edit_data.rights[vitem.code])
                delete $scope.edit_data.rights[vitem.code];
            else
                $scope.edit_data.rights[vitem.code] = vitem;
        }

        $scope.tab = function(k) {
            if (!$('a[data-tab="' + k + '"]').hasClass("active")) {
                $('a[data-tab="' + k + '"]').siblings().removeClass("active");
                $('a[data-tab="' + k + '"]').addClass("active");
                $('a[data-tab="' + k + '"]').parent().siblings('.active').removeClass("active");
                $('a[data-tab="' + k + '"]').parent().siblings('div[data-tab="' + k + '"]').addClass("active");
            }
        }

        $scope.showAdd = function() {
            function addRight(data) {
                $scope.busy = dataFactoryCRM.postDataByAPI('right/add', data)
                    .then(function(d) {
                        if (d && d.rcode) {
                            $scope.add_data = { right_name: '', code: '', info: '', rights: [] };
                            $scope.getDataList();
                            $('.addchk').each(function() {
                                $(this).prop("checked", false);
                            });
                            $('.ui.rights.add.modal').modal('hide');
                        } else {
                            alert(d.msg);
                        }
                        console.log(data);
                    });
            }
            $scope.add_data.rights = {};
            $('.ui.rights.add input').val('');
            $('#frm-add .ui.basic.red').remove();
            $scope.tab("comm");
            $('.ui.rights.add.modal').modal({
                onApprove: function() {
                    if ($('#frm-add').form('is valid')) {
                        addRight($scope.add_data);
                    }
                    return false;
                }
            }).modal('show');
        }

        $scope.nodeDel = function(vobj) {
            if (confirm("确定删除权限集合 " + vobj.right_name)) {
                $scope.busy = dataFactoryCRM.postDataByAPI('right/delete', { id: vobj.right_id })
                    .then(function(d) {
                        if (d.rcode) {
                            $scope.getDataList();
                        } else {
                            alert(d.msg);
                            return false;
                        }
                    });
            }
        }

        $scope.nodeEdit = function(id) {
            function editRight(data) {
                $scope.busy = dataFactoryCRM.postDataByAPI('right/edit', data)
                    .then(function(d) {
                        if (d && d.rcode) {
                            $scope.edit_data = {};
                            $scope.getDataList();
                            $('.ui.rights.edit.modal').modal('hide');
                        } else {
                            alert(d.msg);
                        }
                    });
            }
            $("input[name='edit[rights][]']").each(function() { $(this).prop('checked', false); });

            $scope.busy = dataFactoryCRM.getDataByAPI('right/edit?id=' + id)
                .then(function(d) {
                    if (d.rcode) {
                        $scope.edit_data = d.data;
                        $scope.edit_data.right_id = id;
                        $scope.edit_data.right_name = d.data.right_name;
                        $scope.edit_data.code = d.data.code;
                        $scope.edit_data.info = d.data.info;
                        $scope.edit_data.rights = d.data.right_content;
                        if ($scope.edit_data && $scope.edit_data.rights) {
                            $(".editchk").each(function() {
                                var vchk = $(this).val();
                                if ($scope.edit_data.rights[vchk]) {
                                    $(this).prop("checked", true);
                                }
                            });
                        }
                    } else {
                        alert(d.msg);
                        return false;
                    }
                });
            $scope.tab("comm");
            $('.ui.rights.edit.modal').modal({
                onApprove: function() {
                    if ($('#frm-edit').form('is valid')) {
                        editRight($scope.edit_data);
                    }
                    return false;
                }
            }).modal('show');
        }
        window.loadClientCacheToScope($scope, function(){
            $scope.getDataList();
            $scope.initParam();
        });
        
    });
});